﻿using Thrift;
using Thrift.Protocol;
using Thrift.Server;
using Thrift.Transport;
using Evernote.EDAM.Type;
using Evernote.EDAM.UserStore;
using Evernote.EDAM.NoteStore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsTest
{
	public partial class Form1 : Form
	{
		String authToken;
		NoteStore.Client noteStore;
		Notebook defaultNotebook;
		public Form1()
		{
			InitializeComponent();



			// get the consumerkey and secret by requesting an API Key at http://www.evernote.com/about/developer/api/
            string consumerKey = "YOURKEYHERE";
			string consumerSecret = "YOURSECRETHERE";
			
			// any valid username and pwd on the sandbox.evernote.com service
            string username = "YOURUSERNAMEHERE";
            string password = "YOURPWDHERE";





			string edamBaseUrl = "https://sandbox.evernote.com";
			string userStoreUrl = "https://sandbox.evernote.com/edam/user";
			TTransport userStoreTransport = new THttpClient(userStoreUrl);
			TProtocol userStoreProtocol = new TBinaryProtocol(userStoreTransport);
			UserStore.Client userStore = new UserStore.Client(userStoreProtocol);

			//--Check if the version of EDAM is up to dage
			bool versionOK =
				userStore.checkVersion("C# EDAMTest",
				   Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MAJOR,
				   Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MINOR);
			ShowMessage("Is my EDAM protocol version up to date?  " +
					  versionOK);
			if (!versionOK)			
				return;
			
			//--login user
			AuthenticationResult authResult =
				userStore.authenticate(username, password,
										   consumerKey, consumerSecret);
			
			//--Get user details and authentication token
			User user = authResult.User;
			authToken = authResult.AuthenticationToken;
			ShowMessage("Authentication successful for: " + user.Username);
			ShowMessage("Authentication token: " + authToken);

			String noteStoreUrl = edamBaseUrl + "/edam/note/" + user.ShardId;
			TTransport noteStoreTransport = new THttpClient(noteStoreUrl);
			TProtocol noteStoreProtocol = new TBinaryProtocol(noteStoreTransport);
			noteStore = new NoteStore.Client(noteStoreProtocol);		
	
			//--Get all notebooks in order to get default notebook (Jan 1. 1970)
			List<Notebook> notebooks = noteStore.listNotebooks(authToken);
			ShowMessage("Found " + notebooks.Count + " notebooks:");
			defaultNotebook = notebooks[0];
			foreach (Notebook notebook in notebooks)
			{
				ShowMessage("  * " + notebook.Name);
				if (notebook.DefaultNotebook)				
					defaultNotebook = notebook;				
			}
		}

		/// <summary>
		/// Lists the first ten notes in each notebook
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void listNotes_button_Click(object sender, EventArgs e)
		{
			List<Notebook> notebooks = noteStore.listNotebooks(authToken);
			ShowMessage("Found " + notebooks.Count + " notebooks:");
			
			foreach (Notebook notebook in notebooks)
			{
				ShowMessage("  * " + notebook.Name);
				
				//--Create a filter for retrieving notes
				NoteFilter filter = new NoteFilter();
				//--Set guid of notebook to only retrieve notes for the current notebok
				filter.NotebookGuid = notebook.Guid;
				
				//--Get notes
				NoteList notes = noteStore.findNotes(authToken, filter, 0, 10);
				
				//--List note titles
				foreach (Note note in notes.Notes) 
				{
					ShowMessage("Found note: " + note.Title);
				}
			}
		}
		/// <summary>
		/// Helper method for displaying debug information
		/// </summary>
		/// <param name="message">Message to display</param>
		private void ShowMessage(string message)
		{
			Console.WriteLine(message);
			statusBox.SuspendLayout();
			statusBox.Text += message + "\r\n";
			statusBox.Select(statusBox.Text.Length, 0);
			statusBox.ScrollToCaret();
			statusBox.ResumeLayout();
		}

		/// <summary>
		/// Creates an Hello evernote note in the default notebook
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void createNote_Button_Click(object sender, EventArgs e)
		{
			ShowMessage("Creating a note in the default notebook: " +					  defaultNotebook.Name);
			
			Note note = new Note();
			//--set the guid of the notebook to add the new note to
			note.NotebookGuid = defaultNotebook.Guid;
			ShowMessage("Note title?  ");
			
			//--Set the note title
			note.Title = "Hello Evernote";
			ShowMessage("Well-formed XHTML note content?  ");
			//--Set the content of the note
			note.Content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
				"<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml.dtd\">" +
				"<en-note>" +
				" Hello evernote. This is your note" +
				"</en-note>";
			//--Set note dates in seconds since UNIX epoc (
			DateTime epoch = new DateTime(1970, 1, 1);
			TimeSpan systemTime = DateTime.Now - epoch;
			note.Created = (long)systemTime.TotalMilliseconds;
			note.Updated = note.Created;

			Note createdNote = noteStore.createNote(authToken, note);

			ShowMessage("Created Note: " + createdNote);
		}

		
	}
}
